var express = require('express');
var router = express.Router();
var mysql = require('./mysqlConfig');
var bodyParser = require("body-parser");
// var redis = require('./redisServer');
var register = require('babel-core/register')
var uuid = require('node-uuid')
router.use(bodyParser.json({
    limit: "20000kb"
}));
router.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit: '20000kb',
    extended: true
}));

/* GET home page. */

//处理连接数据库
const connectionMysql = (sql) => {
    return new Promise(function(resolve, reject) {
        mysql.getConnection(function(err, connection) {
            if (err) {
                reject(err)
                console.log(err)
            } else {
                resolve(sqlSever(connection, sql))
            }
        });
    });
}
//处理sql语句
const sqlSever = (connection, sql) => {
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, rows, fields) {
            if (err) {
                console.log(err)
                reject(err);
            } else {
                console.log(err)
                resolve(rows);
                connection.release();
            }
        })
    });
}

//获取所有文章列表
router.get('/getAllArticle', function(req, res, next) {

    var sql = 'select * from cms_article';
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});
//获取文章详情
router.get('/getArticleDetail', function(req, res, next) {
    var ARTICLE_ID = "'" + req.query.id + "'";
    // 'select ac.* ,m.nickname ,m.mem_img from activity_comment ac left join m_member m on m.mobile = ac.mem_id where acti_no=' + acti_no;
    var sql = 'SELECT ac.*, m.* FROM cms_article_content ac LEFT JOIN cms_article m ON m.ARTICLE_ID = ac.ARTICLE_ID WHERE m.ARTICLE_ID = ' + ARTICLE_ID;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});



//获取活动信息
router.get('/getActivityDetail', function(req, res, next) {
    var id = "'" + req.query.id + "'";
    var sql = 'select * from activities where acti_no = ' + id;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {});
});

//活动报名
router.get('/registration', function(req, res, next) {
    var id = "'" + req.query.id + "'";
    var actNum = "'" + req.query.actNum + "'";
    var sql = 'insert into event_registration (acti_no,mem_id) values (' + actNum + ',' + id + ')';
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {
            var json = {
                code: 400

            }
            res.send(json);
        });
});
//取消活动报名
router.get('/cancelRegistration', function(req, res, next) {
    var id = "'" + req.query.id + "'";
    var actNum = "'" + req.query.actNum + "'";
    var sql = 'delete from event_registration where acti_no = ' + actNum + ' and mem_id=' + id;
    console.log(sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {
            var json = {
                code: 400

            }
            res.send(json);
        });
});
//查询活动报名
router.get('/getRegistrationMember', function(req, res, next) {
    var actNum = "'" + req.query.actNum + "'";
    var sql = 'select * from event_registration where acti_no=' + actNum;
    connectionMysql(sql)
        .then((result) => {
            var memlist = '(';
            for (var i = 0; i < result.length; i++) {
                memlist += result[i].mem_id;
                if (i != result.length - 1) {
                    memlist += ',';
                }
            }
            memlist += ')';
            var ssql = 'select mem_img,mobile,id from m_member where mobile in' + memlist;
            console.log("====" + ssql)
            connectionMysql(ssql)
                .then((result) => {
                    var json = {
                        code: 200,
                        data: result
                    }
                    res.send(json);
                })
                .catch((reason) => {
                    var json = {
                        code: 400

                    }
                    res.send(json);
                });

        })
        .catch((reason) => {
            var json = {
                code: 400
            }
            res.send(json);
        });
});
//活动评论
router.post('/activityComments', function(req, res, next) {
    var mem_id = "'" + req.body.mem_id + "'";
    var acti_no = "'" + req.body.acti_no + "'";
    var content = "'" + req.body.content + "'";
    var uuidd = "'" + uuid.v4() + "'";
    var sql = 'insert into activity_comment (comm_no,comm_type,acti_no,mem_id,content) values (' + uuidd + ' ,1 ,' + acti_no + ',' + mem_id + ',' + content + ')';
    console.log("&&&&" + sql)
    connectionMysql(sql)
        .then((result) => {
            var json = {
                code: 200,
                data: result
            }
            res.send(json);
        })
        .catch((reason) => {
            var json = {
                code: 400
            }
            res.send(json);
        });
});
//获取活动评论
router.get('/getActivityComments', function(req, res, next) {
    var acti_no = "'" + req.query.acti_no + "'";
    var sql = 'select ac.* ,m.nickname ,m.mem_img from activity_comment ac left join m_member m on m.mobile = ac.mem_id where acti_no=' + acti_no;
    connectionMysql(sql)
        .then((result) => {
            console.log(result)
            var json = {
                code: 200,
                data: result
            }
            res.send(json);

        })
        .catch((reason) => {

            var json = {
                code: 400
            }
            res.send(json);
        });
});
module.exports = router;